package com.deluxelx.soul.inn.common.util;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 时间处理工具类
 *
 * @author: zihao.liao
 * @date: 2024/1/17 11:06
 */
public class DateTimeUtil {

    public static final String PATTERN_1 = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_2 = "yyyy-MM-dd";

    public static final String PATTERN_3 = "MM-dd HH:mm:ss";

    public static final String PATTERN_4 = "MM-dd";

    public static final String PATTERN_5 = "yyyy-MM-dd HH:mm";

    public static final String PATTERN_6 = "HH:mm";

    public static final long SECOND = 1000L;

    public static final long MINUTE = 60 * SECOND;

    public static final long HOUR = 60 * MINUTE;

    public static final long DAY = 24 * HOUR;

    public static String getStandardDateTime(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern(PATTERN_1));
    }

    public static String getStandardDate(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern(PATTERN_2));
    }

    public static String getTime(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern(PATTERN_6));
    }

    public static String getTimeForHuman(LocalDateTime dateTime) {
        Instant instant = dateTime.toInstant(ZoneOffset.UTC);
        long timeValue = instant.toEpochMilli();
        long now = System.currentTimeMillis();
        long duration = now - timeValue;
        if (duration <= MINUTE) {
            return "刚刚";
        } else if (duration < HOUR) {
            return String.format("%d分钟前", (int) duration / MINUTE);
        } else if (duration < DAY ) {
            return String.format("%d小时前", (int) duration / HOUR);
        } else if (duration < DAY * 2) {
            return "昨天";
        } else if (duration < DAY * 3) {
            return "前天";
        } else {
            int timeYear = dateTime.getYear();
            int currentYear = LocalDateTime.now().getYear();
            SimpleDateFormat formatter;
            if (timeYear == currentYear) {
                formatter = new SimpleDateFormat(PATTERN_4);
            } else {
                formatter = new SimpleDateFormat(PATTERN_2);
            }
            return formatter.format(new Date(timeValue));
        }
    }

    public static String getChatTimeStr(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        int year = dateTime.getYear();
        int yearNow = LocalDateTime.now().getYear();
        if (year == yearNow) {
            return dateTime.format(DateTimeFormatter.ofPattern(PATTERN_3));
        } else if (year < yearNow) {
            return dateTime.format(DateTimeFormatter.ofPattern(PATTERN_1));
        }
        return null;
    }

    public static Long getEmploymentTimeYear(LocalDateTime dateTime) {
        return ChronoUnit.YEARS.between(dateTime, LocalDateTime.now());
    }

    public static String getAppointmentCreateTime(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern(PATTERN_5));
    }

}
